package cn.iocoder.yudao.module.member.user.dal.mysql;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.framework.business.basic.dao.mapper.CoreMapper;
import cn.iocoder.yudao.framework.business.basic.dao.query.CoreLambdaQueryWrapper;
import cn.iocoder.yudao.framework.common.exception.KnownServiceException;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.member.user.dal.dataobject.MemberUserDO;
import cn.iocoder.yudao.module.member.user.vo.MemberUserPageReqVO;
import cn.iocoder.yudao.module.member.user.vo.MemberUserPickReqVO;
import org.apache.ibatis.annotations.Mapper;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 用户 User Mapper
 *
 * @author 芋道源码
 */
@Mapper
public interface MemberUserMapper extends CoreMapper<MemberUserDO> {

    default String selectMobile(Long id) {
        CoreLambdaQueryWrapper<MemberUserDO> wrapper = new CoreLambdaQueryWrapper<>();
        wrapper.select(MemberUserDO::getMobile);
        wrapper.eq(MemberUserDO::getId, id);
        MemberUserDO entity = selectOne(wrapper);
        return entity == null ? "" : entity.getMobile();
    }

    default Long selectPid(Long id) {
        CoreLambdaQueryWrapper<MemberUserDO> wrapper = new CoreLambdaQueryWrapper<>();
        wrapper.select(MemberUserDO::getPid);
        wrapper.eq(MemberUserDO::getId, id);
        MemberUserDO entity = selectOne(wrapper);
        return entity == null ? 0L : NumberUtil.nullToZero(entity.getPid());
    }

    default Set<Long> selectIdsByPid(Long id) {
        CoreLambdaQueryWrapper<MemberUserDO> wrapper = new CoreLambdaQueryWrapper<>();
        wrapper.select(MemberUserDO::getId);
        wrapper.eq(MemberUserDO::getPid, id);
        List<MemberUserDO> entities = selectList(wrapper);
        if (CollUtil.isEmpty(entities)) {
            return Set.of();
        }
        return entities.stream().map(MemberUserDO::getId).collect(Collectors.toSet());
    }

    default MemberUserDO selectByNo(String no) {
        return selectOne(MemberUserDO::getNo, no);
    }

    default MemberUserDO selectByUsername(String username) {
        throw new KnownServiceException("暂不支持“用户名”查询：" + username);
    }

    default MemberUserDO selectByMobile(String mobile) {
        return selectOne(MemberUserDO::getMobile, mobile);
    }

    default List<MemberUserDO> selectListByMobileLike(String mobile) {
        return selectList(new CoreLambdaQueryWrapper<MemberUserDO>()
                .likeIfPresent(MemberUserDO::getMobile, mobile));
    }

    default List<MemberUserDO> selectListByNicknameLike(String nickname) {
        return selectList(new CoreLambdaQueryWrapper<MemberUserDO>()
                .likeIfPresent(MemberUserDO::getNickname, nickname));
    }

    default PageResult<MemberUserDO> selectPage(MemberUserPageReqVO reqVO) {
        // 处理 tagIds 过滤条件
        String tagIdSql = "";
        if (CollUtil.isNotEmpty(reqVO.getTagIds())) {
            tagIdSql = reqVO.getTagIds().stream()
                    .map(tagId -> "FIND_IN_SET(" + tagId + ", tag_ids) > 0")
                    .collect(Collectors.joining(" OR "));
        }
        // 分页查询
        return selectPage(reqVO, new CoreLambdaQueryWrapper<MemberUserDO>()
                .likeIfPresent(MemberUserDO::getMobile, reqVO.getMobile())
                .betweenIfPresent(MemberUserDO::getLoginDate, reqVO.getLoginDate())
                .likeIfPresent(MemberUserDO::getNickname, reqVO.getNickname())
                .betweenIfPresent(MemberUserDO::getCreateTime, reqVO.getCreateTime())
                .eqIfPresent(MemberUserDO::getLevelId, reqVO.getLevelId())
                .apply(StrUtil.isNotEmpty(tagIdSql), tagIdSql)
                .orderByDesc(MemberUserDO::getId));
    }

    default Long selectCountByLevelId(Long levelId) {
        return selectCount(MemberUserDO::getLevelId, levelId);
    }

    default Long selectCountByTagId(Long tagId) {
        return selectCount(new CoreLambdaQueryWrapper<MemberUserDO>()
                .apply("FIND_IN_SET({0}, tag_ids) > 0", tagId));
    }

    default List<MemberUserDO> selectList(MemberUserPickReqVO reqVO) {
        if (StrUtil.isBlank(reqVO.getMobile()) && StrUtil.isBlank(reqVO.getMobile())) {
            return new ArrayList<>();
        }
        return selectList(new CoreLambdaQueryWrapper<MemberUserDO>()
                .likeIfPresent(MemberUserDO::getMobile, reqVO.getMobile())
                .likeIfPresent(MemberUserDO::getNickname, reqVO.getNickname()));
    }

}
